//
//  RootViewController.m
//  MyTreeViewPrototype
//
//  Created by Jon Limjap on 4/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LeftTreeView.h"
#import "MyTreeViewCell.h"
#import "KakaoLinkCenter.h"

@implementation LeftTreeView
@synthesize leftTrdelegate;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

	treeNode = [[MyTreeNode alloc] initWithValue:@"Root"];
	MyTreeNode *node1 = [[MyTreeNode alloc] initWithValue:@"프리미엄벽지"];
	[treeNode addChild:node1];
	
	MyTreeNode *node1a = [[MyTreeNode alloc] initWithValue:@"지아벽지"];
	MyTreeNode *node1b = [[MyTreeNode alloc] initWithValue:@"베라왕"];
    MyTreeNode *node1c = [[MyTreeNode alloc] initWithValue:@"애냐라킨"];
    MyTreeNode *node1d = [[MyTreeNode alloc] initWithValue:@"뮤럴벽지"];

	[node1 addChild:node1a];
	[node1 addChild:node1b];
    [node1 addChild:node1c];
    [node1 addChild:node1d];
    
    MyTreeNode *node2 = [[MyTreeNode alloc] initWithValue:@"실크벽지"];
	[treeNode addChild:node2];
	
	MyTreeNode *node2a = [[MyTreeNode alloc] initWithValue:@"공기를 살리는 벽지"];
	MyTreeNode *node2b = [[MyTreeNode alloc] initWithValue:@"아이비리그"];
    MyTreeNode *node2c = [[MyTreeNode alloc] initWithValue:@"실크NB"];
    MyTreeNode *node2d = [[MyTreeNode alloc] initWithValue:@"실크테라피"];
    MyTreeNode *node2e = [[MyTreeNode alloc] initWithValue:@"방염벽지"];

	[node2 addChild:node2a];
	[node2 addChild:node2b];
    [node2 addChild:node2c];
    [node2 addChild:node2d];
    [node2 addChild:node2e];
    
    MyTreeNode *node3 = [[MyTreeNode alloc] initWithValue:@"합지벽지"];
	[treeNode addChild:node3];
    MyTreeNode *node3a = [[MyTreeNode alloc] initWithValue:@"휘앙새"];
	[node3 addChild:node3a];

	node1.inclusive = NO;
    node2.inclusive = NO;
    node3.inclusive = NO;
//	node1b.inclusive = NO;
    
	MyTreeNode *node4 = [[MyTreeNode alloc] initWithValue:@"벽타일"];
	[treeNode addChild:node4];
    node4.inclusive = NO;
	
	MyTreeNode *node4a = [[MyTreeNode alloc] initWithValue:@"공기룰 살리는 숨타일"];
	[node4 addChild:node4a];
       
    MyTreeNode *node9 = [[MyTreeNode alloc] initWithValue:@"MY샘플북"];
	[treeNode addChild:node9];
//        MyTreeNode *node8 = [[MyTreeNode alloc] initWithValue:@""];
//    	[treeNode addChild:node8];
    
//    MyTreeNode *node4 = [[MyTreeNode alloc] initWithValue:@"MY샘플북"];
//	[treeNode addChild:node4];
    
    
//	MyTreeNode *node2a1 = [[MyTreeNode alloc] initWithValue:@"Node2a1"];
//	[node2a addChild:node2a1];
    
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    descendantCount = descendantCount +1;
    NSLog(@"%d",[treeNode descendantCount]);
//    self.contentSizeForViewInPopover = CGSizeMake(self.view.frame.size.width, [treeNode descendantCount]*44 + 100);
    return [treeNode descendantCount];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
	
   	MyTreeNode *node = [[treeNode flattenElements] objectAtIndex:indexPath.row + 1];
	MyTreeViewCell *cell = [[MyTreeViewCell alloc] initWithStyle:UITableViewCellStyleDefault
												 reuseIdentifier:CellIdentifier 
														   level:[node levelDepth] - 1 
														expanded:node.inclusive];   
	
//    UIImage* aaa = [UIImage imageNamed:@"x"];
//    UIImageView* aaaa = [[UIImageView alloc] initWithImage:aaa];
//    aaaa.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
//    [cell addSubview:aaaa];
    cell.valueLabel.text = node.value;
    return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	MyTreeNode *node = [[treeNode flattenElements] objectAtIndex:indexPath.row + 1];
    
	if (!node.hasChildren)
    {
        [self.leftTrdelegate leftTreeViewSelect:indexPath.row haschilden:YES];
        [tableView reloadData];
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setSelected:NO animated:YES];

        return;
    }
    else 
    {
        node.inclusive = !node.inclusive;	
        [treeNode flattenElementsWithCacheRefresh:YES];
        [self.leftTrdelegate leftTreeViewSelect:indexPath.row haschilden:NO];
        [tableView reloadData];
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setSelected:NO animated:YES];

    }
    
    
    /////
//    
//    NSString *message = @"카카오링크를 사용하여 메시지를 전달해보세요."; // 메세지 
//    NSString *referenceURLString = @"http://link.kakao.com"; // 링크 넘기기
//    NSString *appBundleID = @"com.example.app";
//    NSString *appVersion = @"2.0";
//    NSString *appName = @"example"; // 제목
//    
//    if ([[KakaoLinkCenter defaultCenter] canOpenKakaoLink]) {
//        [[KakaoLinkCenter defaultCenter] openKakaoLinkWithURL:referenceURLString 
//                                                   appVersion:appVersion
//                                                  appBundleID:appBundleID
//                                                      appName:appName
//                                                      message:message];
//    } else {
//        // 카카오톡이 설치되어 있지 않은 경우에 대한 처리
//        NSLog(@"카카오톡 없음요");
//    }
    
    
//    NSLog(@"%d",indexPath.row);
    
	}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

